{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 13.2 DBSCAN算法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 13.2.2 DBSCAN算法的代码实现"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10.44</td>\n",
       "      <td>5.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11.55</td>\n",
       "      <td>6.16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11.36</td>\n",
       "      <td>5.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.62</td>\n",
       "      <td>6.12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>11.20</td>\n",
       "      <td>5.39</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       x     y\n",
       "0  10.44  5.74\n",
       "1  11.55  6.16\n",
       "2  11.36  5.10\n",
       "3  10.62  6.12\n",
       "4  11.20  5.39"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "data = pd.read_excel('演示数据.xlsx')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.数据可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.scatter(data.iloc[:, 0], data.iloc[:, 1], c=\"green\", marker='*')  # 以绿色星星样式绘制散点图\n",
    "plt.xlabel('x')  # 添加x轴名称\n",
    "plt.ylabel('y')  # 添加y轴名称\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.数据建模"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import DBSCAN\n",
    "dbs = DBSCAN()\n",
    "dbs.fit(data)\n",
    "label_dbs = dbs.labels_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# DBSCAN?  # 如果想查看DBSCAN的官方说明，可以在其DBSCAN后面加上?进行查看"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.查看聚类结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 1 0 1 0 1 1 0 0 0 0 1 0 0 0 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 0 1 1 1 0\n",
      " 1 0 0 0 1 0 1 0 1 0 0 1 0 1 0 0 1 0 0 0 1 1 1 0 1 1 0 0 0 0 0 1 1 0 0 0 1\n",
      " 1 1 1 1 1 0 1 0 0 1 0 0 1 0 1 1 1 1 0 1 1 1 0 1 1 0]\n"
     ]
    }
   ],
   "source": [
    "print(label_dbs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.用散点图展示DBSCAN算法的聚类结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x267fa9bba20>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEKCAYAAAD9xUlFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzt3Xt0VPXZL/DvM7mSBEEuUjUkAVFBUFCCVDhS0FrUurBVZAlpkeopx/f1tlYrb+3iWCUY8bZqVfqWovZoDwEJXnmrora0rz1vW0io3IJyAOUSEMWgQQ8GQvKcP/bMMJnMTuayfzN77/l+1po1ycyePb/szPye/bs9W1QVREREABDIdAGIiMg9GBSIiCiMQYGIiMIYFIiIKIxBgYiIwhgUiIgojEGBiIjCGBSIiCiMQYGIiMJyM12ARA0YMEArKioyXQwiIk/ZsGHDZ6o6sKftPBcUKioq0NDQkOliEBF5iojsiWc7dh8REVEYgwIREYUxKBARUZjnxhSIiGJpa2tDU1MTWltbM12UjCosLERpaSny8vKSej2DAhH5QlNTE3r37o2KigqISKaLkxGqiubmZjQ1NWHIkCFJ7YPdR0TkC62trejfv3/WBgQAEBH0798/pdYSgwIR+UY2B4SQVI8BgwJ5U20tUFEBBALWfW1tpksUm1fKSRTEoEDeU1sLzJ0L7NkDqFr3c+e6r8L1SjmdxCDYyf3334/HHnvMsf2tWbMG5557LoYNG4aHHnrIsf1GYlAg75k/Hzh6tPNjR49aj7uJV8rplGwMgmnU3t6O2267DW+++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gpqCj/lVPMcEAApFpEFE/iEiWRc4bGTbZyURCuBtEdkQzC6QLW4G8GaMx339WfH7ldfiSqORhcpU9YCIDAWwVkS2qOquTBcqw/hZsTcx+Hk5DcA7IvJB8Czbt0RkPqxsDLWxno7xmG8+K15uKXwiIqcDQPD+0xjbNAEYHPF7KYADaShbpsRzTKCqB4L3H8JaP3JhugroYtn2WYlbxOflUwCvwOo+8a3ghJRrAFTZZFjw9WfFy0EhMkXGTQBei7HNWwC+IyKnBgeYvxN8zK96PCbBY1EQ/HkAgIkAtqWthO5VD+BsERkiIvmwVtf7amZWMkSkWER6h36G9R3a2v2rvEtErgTwMwDTVPWozWb+/qyoqutvAFYA+BhAG6wofQusFNt/ArAjeN8vuG0lgGciXnszgJ3B248y/bdk+pgAmABgC4BNwftbMv23pOG4fD/48zEAnwB4K7jtGQDeiHjt1bBmnOwCMD/Tf4sbjguAocHPyiYAjX46LjbHZCes8YKNwduSbPuscEUzERGFebn7iIiIHMagQEREYQwKREQUxqBARERhDApERBTGoEBERGEMCkREFMagQJQiERkXzL9fGFwB3CgiozJdLqJkcPEakQNE5AEAhQB6AWhS1UUZLhJRUhgUiBwQzIFTD6AVwARVbc9wkYiSwu4jImf0A1ACoDesFgORJ7GlQOSA4LWuXwAwBMDpqnp7hotElBS/X2SHyDgRmQ3ghKouD16/928icpmqrs102YgSxZYCERGFcUyBiIjCGBSIiCiMQYGIiMIYFIiIKIxBgYiIwhgUiIgojEGBiIjCGBSIiCjs/wPa+bpTPFDpuQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(data[label_dbs == 0].iloc[:, 0], data[label_dbs == 0].iloc[:, 1], c=\"red\", marker='o', label='class0')  # 以红色圆圈样式绘制散点图并加上标签  \n",
    "plt.scatter(data[label_dbs == 1].iloc[:, 0], data[label_dbs == 1].iloc[:, 1], c=\"green\", marker='*', label='class1')  # 以绿色星星样式绘制散点图并加上标签 \n",
    "plt.xlabel('x')  # 添加x轴名称\n",
    "plt.ylabel('y')  # 添加y轴名称\n",
    "plt.legend()  # 设置图例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 13.2.3 KMeans VS DBSCAN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import KMeans\n",
    "KMs = KMeans(n_clusters=2)\n",
    "KMs.fit(data)\n",
    "label_kms = KMs.labels_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# KMs # 这样可以查看模型参数，这里没有设置random_state参数，所以可能每次跑出来的结果略有不同（因为每次起始点选的地方不同）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 1 1 0 0 0 1 1 1 0 0 0 1 1 0 0 1 1 0 1 1\n",
      " 0 0 1 0 1 0 0 1 0 1 0 0 1 1 0 1 0 1 1 0 0 1 1 1 0 1 1 0 0 0 1 0 1 0 0 1 0\n",
      " 1 0 1 1 1 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0]\n"
     ]
    }
   ],
   "source": [
    "print(label_kms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x267f82aeef0>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEKCAYAAAD9xUlFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUA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5/dhTSy8LFsF9Khc+54kLeK78imAV2A1n/hWaDDKTQDqNfaYfV9/V0wPClsADIwmWgrg1zH2eQPAd0Xk7FAH83dD2/xq2HMSOhcjQz+Pg5W8sDVrJfSmnQAuFJFJIjICVsoV34zKSpeIlIjI6IGfYf3/2Zf4XeYSkesB/BBAjaqejLObv78rqmrEA8AGAB8D6IEVqe+AtfbC7wC0h57HhvatAvDziPfeDuBA6HGb2/8Wt88JgDkA3gawJ/R8h9v/FofPyd+Ffj4N4BMAb4T2PQ/Atoj33ghrxMl7AFa6/W/xwnkBcH7oe7IHwH4/nZc45+QArP6C3aHHmlz7rnBGMxERhZnefERERDZiUCAiojAGBSIiCmNQICKiMAYFIiIKY1AgIqIwBgUiIgpjUCDKkIhUh/LvF4VmAO8Xkelul4soHZy8RmQDEXkIQBGAUQA6VfVhl4tElBYGBSIbhHLg7ARwCsAcVe1zuUhEaWHzEZE9xgIIABgNq8ZAZCTWFIhsEFrn+gUAkwCcq6orXC4SUVpyYZEdIkeJyBIAvaq6PrR+75sicrWqbne7bESpYk2BiIjC2KdARERhDApERBTGoEBERGEMCkREFMagQEREYQwKREQUxqBARERhDApERBT2/wF2SGRRBA9pWQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(data[label_kms == 0].iloc[:, 0], data[label_kms == 0].iloc[:, 1], c=\"red\", marker='o', label='class0')  # 以红色圆圈样式绘制散点图并加上标签  \n",
    "plt.scatter(data[label_kms == 1].iloc[:, 0], data[label_kms == 1].iloc[:, 1], c=\"green\", marker='*', label='class1')  # 以绿色星星样式绘制散点图并加上标签 \n",
    "plt.xlabel('x')  # 添加x轴名称\n",
    "plt.ylabel('y')  # 添加y轴名称\n",
    "plt.legend()  # 设置图例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到，对于形状类似同心圆的数据，KMeans算法聚类效果较差，只能机械地将数据分为左右两部分，而无法以外圆内圆的方式进行区分。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
